% GAUTIER LE BIHAN - 2020
% Replication files for "Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model?" Review of Economics and Statistics
%
% Table D

clear;
tic
addpath('..\..\Utilities')  
load actual_moments_k

weight=actual_moments_k(:,12);
sum(weight)
load ..\..\Estimation_param\MS_produits_MC\param

s=227

    p0=0*ones(size(param,1),1);
    muc=param(:,1);
    sig_ea=exp(param(:,2));
    rho_a=0.6946*ones(size(param,1),1); 
    



param0=[p0 muc sig_ea rho_a]

size_param=size(param0,2)


stat_med=ones(1,size_param+2);
%figure;hist(param_sect(:,3));
v=size(param0,1);
 if v>0
    for i=1:size(param0,2)
        [sortx,order] = sort(param0(:,i));
        sortw = weight(order);

         midpoint = sum(sortw)/2;
         csumw = cumsum(sortw);
         j = find(csumw<=midpoint,1,'last');
         
        if isempty(j)==1
             j=1;
         end
            if csumw(j) == midpoint
              stat_med(i) = mean(sortx([j j+1]));
            else
                
                stat_med(i) = sortx(j+1);
            end
     stat_med(end)= csumw(end);
     stat_med(end-1)= v;
    end  
 end

stat_med

s=227;
for pp=2:3

param_t=param0;
param_t(:,pp)=stat_med(:,pp);

    stat=zeros(s,16);
    for jj=1:s;
        p0=param_t(jj,1);
        mu_c=param_t(jj,2);
        sig_eps_a=param_t(jj,3);
        rho_a=param_t(jj,4);   
        weight_j=weight(jj);
        sm_m=0;
        param_e=[p0 mu_c sig_eps_a rho_a];
%     moments=[freq  fracup med interq kur absq1 absq3 share_Calvo mu_p MC_paid];
%     VC=(VARrad^(1/2))*100; 
        [moments, VC]=geNCalvoPlus_VC(p0, mu_c, sig_eps_a, rho_a, weight_j);
    %moments=[freq  fracup med interq kur absq1 absq3 share_Calvo mu_p MC_paid];
        stat(jj,:)=[ param_e moments VC];
	jj
       
    end
   stat_simu_out(pp).stat_simu_out=stat;
   save stat_simu_out stat_simu_out;
end